﻿using BridgeShips;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SetAlterData : MonoBehaviour {
    //public enum ShipType {
    //    LineType,
    //    StraddleType
    //}
    //public enum DeviceDir {
    //    UpLeftAndDownRight,
    //    UpRightAndDownLeft
    //}
    public ShipTypes m_shipType;
    public DeviceDir m_deviceDir;

    private Toggle m_shipTypeToggle;
    private Toggle m_deviceTypeToggle;

    private ReadXML m_readXML;
    private string url;
	void Start () {

        m_readXML = new ReadXML();
        url = Application.dataPath + "/Resources/Configs/GameDate.xml";
        m_readXML.XmlCreate(url);
        
        m_shipType = ShipTypes.StraddleType;
        m_deviceDir = DeviceDir.UpLeftAndDownRight;
        GameFunc.Instance.m_ShipTypes = m_shipType;
        m_shipTypeToggle = transform.Find("DataMsgsPanel/ChooseShipType/ShipType_Toggle").GetComponent<Toggle>();
        m_deviceTypeToggle = transform.Find("DataMsgsPanel/ChooseDeviceType/DeviceType_Toggle").GetComponent<Toggle>();
        m_shipTypeToggle.onValueChanged.AddListener(OnShipTypeChoose);
        m_deviceTypeToggle.onValueChanged.AddListener(OnDeviceType);
        string m_QualityValue = m_readXML.XmlRead( "imageQuality", url);
       
        switch (m_QualityValue)
        {
            case "High":
                transform.Find("DataMsgsPanel/imagequalite/Toggle_tall").GetComponent<Toggle>().isOn = true;
                break;
            case "Middle":
                transform.Find("DataMsgsPanel/imagequalite/Toggle_middle").GetComponent<Toggle>().isOn = true;
                break;
            case "Low":
                transform.Find("DataMsgsPanel/imagequalite/Toggle_under").GetComponent<Toggle>().isOn = true;
                break;
            default:
                break;
        }
    }
   
	// Update is called once per frame
	void Update () {
        if (m_shipTypeToggle.interactable!=false)
        {
            int m_ShipCount = ShipsDictionary.Instance.shipDic.Count;
            if (m_ShipCount>0)
            {
                m_shipTypeToggle.interactable = false;
                m_shipTypeToggle.transform.parent.Find("Toggle (1)").GetComponent<Toggle>().interactable = false;
            }
        }
    }

    public void Onclick() {
        //获取按钮中哪个Toggle被选择了
        IEnumerable<Toggle> toggleGroup = GameObject.Find( "DataMsgsPanel/imagequalite" ).GetComponent<ToggleGroup>().ActiveToggles();
        foreach (Toggle item in toggleGroup) {
            if (item.isOn) {
                switch (item.name) {
                    case "Toggle_under":
                        //修改画质
                        QualitySettings.SetQualityLevel( 1, true );
                        m_readXML.XmlUpdate(url, "imageQuality", "Low");
                        break;
                    case "Toggle_middle":
                        QualitySettings.SetQualityLevel( 3, true );
                        m_readXML.XmlUpdate(url, "imageQuality", "Middle");
                        break;
                    case "Toggle_tall":
                        QualitySettings.SetQualityLevel( 5, true );
                        m_readXML.XmlUpdate(url, "imageQuality", "High");
                        break;
                }
                break;
            }
        }
    }

    private void OnShipTypeChoose(bool isOn) {
        if (isOn)
        {
            m_shipType = ShipTypes.StraddleType;
            m_readXML.XmlUpdate(url, "choose", "prefab");
        }
        else
        {
            m_shipType = ShipTypes.LineType;
            m_readXML.XmlUpdate(url, "choose", "line");
        }
        GameFunc.Instance.m_ShipTypes = m_shipType;
    }
    private void OnDeviceType(bool isOn) {
        if (isOn)
        {
            m_deviceDir = DeviceDir.UpLeftAndDownRight;
            m_readXML.XmlUpdate(url, "locatorType", "Left");
        }
        else
        {
            m_deviceDir = DeviceDir.UpRightAndDownLeft;
            m_readXML.XmlUpdate(url, "locatorType", "Right");
        }
    }
}
